package com.keyancompany.zhongbaopingtai.activity;

import android.content.Intent;
import android.support.v7.internal.VersionUtils;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.AppManager;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.ZBApplication;
import com.keyancompany.zhongbaopingtai.api.UpdateAPI;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.Update;
import com.keyancompany.zhongbaopingtai.model.User;
import com.keyancompany.zhongbaopingtai.utils.AppUtils;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;
import com.keyancompany.zhongbaopingtai.utils.NetUtils;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.MenuPopuWindow;
import com.keyancompany.zhongbaopingtai.weight.UpdateDialog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.photoselector.util.CommonUtils;

import java.io.File;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;

public class SysSettingActivity extends BaseActivity {

    @BindString(R.string.setting_text)
    String title;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.clear_parent)
    LinearLayout clearParent;
    @Bind(R.id.cache_size)
    TextView cacheSize;
    @Bind(R.id.about_textview)
    TextView aboutText;
    @Bind(R.id.feedback_textview)
    TextView feedbackText;
    @Bind(R.id.version_value_text)
    TextView versionText;

    @Bind(R.id.version_parent)
    LinearLayout versionParent;
    //    VersionWarningPopup versionWarningPopup;
    private UpdateDialog updateDialog;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_sys_setting;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {

        versionText.setText(AppUtils.getVersionName(this));
    }


    @OnClick(R.id.clear_parent)
    public void clearCache(View view) {
        clearImage();
    }

    @OnClick(R.id.version_parent)
    public void versionParent(View view) {
//        versionParent

        UpdateAPI.updateApp(this, getLoading(-1));

    }

    @OnClick(R.id.feedback_textview)
    public void toFeedback(View view) {
        IntentUtils.startActivity(mContext, FeedbackActivity.class);
    }

    /***
     * 清除图片缓存
     */
    public void clearImage() {

        ImageLoader.getInstance().clearDiskCache();
        ImageLoader.getInstance().clearMemoryCache();
        T.show(SysSettingActivity.this, "缓存已清空", 0);
    }


    @OnClick(R.id.about_textview)
    public void toAbout(View view) {
        IntentUtils.startActivity(mContext, AboutActivity.class);
    }

    @OnClick(R.id.login_out) //注销
    public void toLoginOut(View view) {
        AppManager.getAppManager().finishAllActivity();
        IntentUtils.startActivity(mContext, LoginActivity.class);
    }

    @Override
    public void failed(String error) {
        super.failed(error);
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        // {"result":"sucess","token":"111111","data":[{"appUrl":"http://test.allinnovate.com:80/CrowdSourcing-Web/
        // appversion/app-release.apk",
        // "pid":"ff8080815b035a82015b035bd0f60001",
        // "appType":"0",
        // "indexNo":1,
        // "version":"1.0.0"}],"msg":"加载成功！"}
        BaseModel base = JSONObject.parseObject(json, BaseModel.class);
        if (!base.isSuccess()) {
            return;
        }
        JSONArray array = JSONObject.parseArray(base.data);
        String obj = array.getString(0);
        Update update = JSONObject.parseObject(obj, Update.class);
        showUpdateDialog(update);

    }

    public void showUpdateDialog(Update update) {
        updateDialog = new UpdateDialog(this);
        updateDialog.updateView(update);
        updateDialog.show();
    }

}
